package view;

import java.util.ArrayList;
import java.util.Scanner;

import model.Employee;
import service.Constan;
import service.EmployeeAPI;
import service.TimesheetAPI;
import service.implement.EmployeeDAO;
import service.implement.TimesheetDAO;

public class EmployeeApp {
	Scanner sc=new Scanner(System.in);
	EmployeeAPI dao=new EmployeeDAO();
	Employee em=new Employee();
	TimesheetAPI ts=new TimesheetDAO();
	public void menuCapnhatEmp(){
		System.out.println("1.Cap nhat theo id");
		System.out.println("2.Cap nhat theo name");
		System.out.println("3.Cap nhat theo username");
		System.out.println("4.Thoat");
	}
	public void menuDeleteEmp(){
		System.out.println("1.Xoa theo id Employee");
		System.out.println("2.Xoa theo name Employee");
		System.out.println("3.Thoat");
	}
	public void menuTimkiemEmp(){
		System.out.println("1.Tim kiem theo Id");
		System.out.println("2.Tim kiem theo ten");
		System.out.println("3.Thoat");
	}
	public void capNhatEmpbyid(){
		try{
			if(dao.updateEmployee(em.updateEmployee())==Constan.SER_STATUS_EROOR){
				System.out.println("Not Update successfull");
			}
			else{
				System.out.println("Update Successfull");
			}
		}
		catch(NullPointerException e){
			System.out.println(" Vui long thu lai!");
		}	
	}
	public void capNhatEmpbyname(){
		System.out.println("Nhap name can tim kiem: ");
		String name=sc.nextLine();
		while(name.trim().equalsIgnoreCase("")){
			System.out.println("Name chua duoc nhap.Nhap lai");
			name=sc.nextLine();
		}
		ArrayList<Employee> em2=dao.getEmployeeByName(name);
		if(dao.getEmployeeByName(name).isEmpty()){
			System.out.println("Name khong ton tai");
		}else{

			for(Employee employee:em2){
				System.out.println(employee);
			}
			capNhatEmpbyid();
		}	
	}
	public void capNhatbyUsername(){
		System.out.println("Nhap username can tim kiem: ");
		String username=sc.nextLine();
		while(username.trim().equalsIgnoreCase("")){
			System.out.println("Username chua duoc nhap.Nhap lai!");
			username=sc.nextLine();
		}
		if(dao.getEmployeeByUsername(username).isEmpty()){
			System.out.println("Khong ton tai username");
		}else{
			ArrayList<Employee> em3=dao.getEmployeeByUsername(username);
			System.out.println(Constan.DISPLAY_EMP);
			for(Employee e:em3){
				System.out.println(e);
			}
			capNhatEmpbyid();
		}
	}
	public void inEmp(){
		ArrayList<Employee> emps = dao.getEmployee();
		System.out.println("=================================Danh sach Employee================================\n");
		System.out.println(Constan.DISPLAY_EMP);
		for (Employee e:emps ) {
			System.out.println("----------------------------------------------------------------------------------");
			System.out.println(e);
		}
	}
	public void nhapEmp(){
		try{
			if(dao.addEmployee(em.inputEmployee())==Constan.SER_STATUS_EROOR){
				System.out.println("Not Add successfull");
			}
			else{
				System.out.println("Add Successfull");
			}
		}
		catch(NullPointerException e){
			System.out.println(" Vui long thu lai!");
		}
	}
	public void deleteEmpById(){
		System.out.println("Nhap id can xoa: ");
		int id=Integer.parseInt(sc.nextLine());
		if(dao.getEmployeeById(id)==null){
			System.out.println("Id khong ton tai");
		}else{
			Employee em=dao.getEmployeeById(id);
			System.out.println("Thong tin id da co:");
			System.out.println(Constan.DISPLAY_EMP);
			System.out.println(em);
			System.out.println("Ban thuc su muon xoa");
			System.out.println("1.Co");
			System.out.println("2.Khong");
			int chon2=Integer.parseInt(sc.nextLine());
			if(chon2==1){
				if(ts.checkEmployeeid(id)){
					System.out.println("Thong tin employee da duoc khai trong timesheet khong the xoa.Vui long xoa timesheet va thu lai");
					return;
				}
				else if(dao.deleteEmployee(id)==Constan.SER_STATUS_SUCSESS){
					System.out.println("Delete Successfull");
				}
			}
			else{
				System.out.println("Xoa khong thanh cong thu lai");
			}
		}
	}
	public void searchEmp(){
		System.out.println("Nhap id can tim kiem: ");
		int id=Integer.parseInt(sc.nextLine());
		if(dao.getEmployeeById(id)==null){
			System.out.println("Khong ton tai id");
		}
		else{
			Employee em=dao.getEmployeeById(id);
			System.out.println(Constan.DISPLAY_EMP);
			System.out.println(em);
		}
	}
	public void searchByName(){
		System.out.println("Nhap name can tim kiem: ");
		String name=sc.nextLine();
		while(name.trim().equalsIgnoreCase("")){
			System.out.println("Name chua duoc nhap.NHap lai");
			name=sc.nextLine();
		}

		if(dao.getEmployeeByName(name).isEmpty()){
			System.out.println("Name khong ton tai");
		}else{
			ArrayList<Employee> em=dao.getEmployeeByName(name);
			System.out.println(Constan.DISPLAY_EMP);
			for(Employee e:em){
				System.out.println(e);
			}
		}

	}
	public void deleteEmpbyName(){
		System.out.println("Nhap name can tim kiem: ");
		String name=sc.nextLine();
		while(name.trim().equalsIgnoreCase("")){
			System.out.println("Name chua duoc nhap.NHap lai");
			name=sc.nextLine();
		}

		if(dao.getEmployeeByName(name).isEmpty()){
			System.out.println("Name khong ton tai");
			return;
		}else{
			ArrayList<Employee> em=dao.getEmployeeByName(name);
			System.out.println(Constan.DISPLAY_EMP);
			for(Employee e:em){
				System.out.println(e);
			}
			deleteEmpById();
		}
	}
}
